package alkaid.eis.sys.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import alkaid.eis.sys.entity.LongText;
import alkaid.eis.sys.repository.LongTextRepository;
import lombok.RequiredArgsConstructor;

/**
 * 长文本服务
 *  
 * @author Lucky Yang
 * @since 0.0.1
 */
@Service
@RequiredArgsConstructor
public class LongTextService {
    private final LongTextRepository longTextRepository;
 
    public String findContentByRefId(Long refId){
        return longTextRepository.findByRefId(refId).map(LongText::getContent).orElseGet(() -> {
            LongText text = new LongText(refId);
            longTextRepository.save(text);
            return text.getContent();
        });
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveContentByRefId(Long refId, String content){
        LongText entity = longTextRepository.findByRefId(refId).orElseGet(() -> new LongText(refId, content));
        
        longTextRepository.save(entity);
    }
}
